package controllers

import (
	"fmt"
	"log"
	"strings"
	"sync"

	"github.com/gocolly/colly"
)

// city := "南京"
func RunCity(city string) {

	log.Println("city----:", city)
	cityKey := ToAllPy(city)
	cityKeyPy := ToPy(city)
	link := fmt.Sprintf("http://%sdt.8684.cn/", ToPyL(city))
	mapimg := fmt.Sprintf("http://img.8684.cn/intro/line/%s.png", cityKey)
	NewCity(link, Metro{Versions: "v0.0.1", City: city, CityKey: cityKey, CityKeyPy: cityKeyPy, Map: mapimg}).Listen()
}

func Run2() {
	log.Println("Start")
	group := new(sync.WaitGroup)
	c := colly.NewCollector()
	c.OnHTML(".line a", func(e *colly.HTMLElement) {
		city := strings.Replace(e.Text, "地铁", "", -1)
		cityKey := ToAllPy(city)
		cityKeyPy := ToPy(city)
		link := e.Attr("href")
		mapimg := fmt.Sprintf("http://img.8684.cn/intro/line/%s.png", cityKey)
		if city == "香港" {
			cityKey = "hongkong"
			cityKeyPy = "HK"
		}

		log.Println("texts", city, cityKey, cityKeyPy, link)
		group.Add(1)
		go func() {
			NewCity(link, Metro{Versions: "v0.0.1", City: city, CityKey: cityKey, CityKeyPy: cityKeyPy, Map: mapimg}).Listen()
			group.Done()
		}()

	})
	c.Visit("http://dt.8684.cn/")

	group.Wait()
	log.Println("End")
}

func Run() {
	log.Println("Start")

	c := colly.NewCollector()
	c.OnHTML(".line a", func(e *colly.HTMLElement) {
		city := strings.Replace(e.Text, "地铁", "", -1)
		cityKey := ToAllPy(city)
		cityKeyPy := ToPy(city)
		link := e.Attr("href")
		mapimg := fmt.Sprintf("http://img.8684.cn/intro/line/%s.png", cityKey)
		if city == "香港" {
			cityKey = "hongkong"
			cityKeyPy = "HK"
		}

		log.Println("texts", city, cityKey, cityKeyPy, link)

		NewCity(link, Metro{Versions: "v0.0.1", City: city, CityKey: cityKey, CityKeyPy: cityKeyPy, Map: mapimg}).Listen()

	})
	c.Visit("http://dt.8684.cn/")

	log.Println("End")
}
